package com.atguigu.tingshu.user.factory;

import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.user.strategy.ItemTypeStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
/**
 * 策略工厂类
 */
@Slf4j
@Component
public class StrategyFactory {
    /**
     * 将IOC容器中ItemTypeStrategy接口下所有实现类对象注入到Map中
     * Map中Key：Bean对象ID
     * Map中Value：实现类对象
     * {"1001":购买类型为专辑策略类对象}
     * {"1002":购买类型为声音策略类对象}
     * {"1003":购买类型为会员策略类对象}
     */
    @Autowired
    private Map<String, ItemTypeStrategy>strategyMap;
    public ItemTypeStrategy getStrategy(String itemType){
        if(strategyMap.containsKey(itemType)){
            return strategyMap.get(itemType);
        }
        log.error("没有对应的策略类");
        throw new GuiguException(500,"该策略"+itemType+"不存在");
    }
}
